clear
est clear
cap mat define migrationfigure=J(105,10,.)
		local row=1

	use "$input/full_geoid_pair_panel.dta"
	
	
			
	replace migration=migration/(tot_pop/100000)
	
	
	
	
	
	

	//Make sure only include states that have a storm in at least one county at least once in our sample years
	bysort state_fips: egen max=max(storm)
		drop if max!=1
		
	//Create future pooled migration - total migrants in the subsequent five years
	sort geoid_pair move_year
	g post_storm=1 if storm==1|storm[_n-1]==1&geoid_pair==geoid_pair[_n-1]|storm[_n-2]==1&geoid_pair==geoid_pair[_n-2]|storm[_n-3]==1&geoid_pair==geoid_pair[_n-3]|storm[_n-4]==1&geoid_pair==geoid_pair[_n-4]|storm[_n-5]==1&geoid_pair==geoid_pair[_n-5]
	mvencode post_storm, mv(0)
	
	//Collapse to total migration from a county (not looking at paired county to county migration, just all migration out of a county)
	
	collapse (sum) migration (mean) share_approved totalapprovedihpamount tot_pop,by(from_geoid move_year storm post_storm state_fips)
	
	//Create IHS transformations
	ihstrans(migration)
	
	
	//Create lags of storms (was there a storm in t-1 years)

			
	label var post_storm "1(0-5 yrs post storm)"
	label var storm "1(Storm year)"
	sort from_geoid move_year
	
	g totalap=totalapprovedihpamount/1000000
		*10MM == 92 ptile
		*20MM == 95 ptile
		*40MM == 97 ptile
		*80MM == 99 ptile
		*160MM == 99.5 ptile

	g totalappc=totalapprovedihpamount/(tot_pop/100000)
		*1.15e+07 == 90 ptile
		*2.29e+07  == 95 ptile
		*3.74e+07 == 97 ptile
		*7.78e+07 == 99 ptile
		*1.31e+08 == 99.5 ptile
	
	drop totalapprovedihpamount totalap
	
	
	g highdamage=(totalapp>1.15e+07 & totalapp!=. & totalapp<2.29e+07) 
	mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
	foreach var in L1 L2 L3 L4 L5{
		label var highdamage_`var' "1(Storm year), `var'"
	}
		
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
		
			eststo t1c1
			
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c1
		
			
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta11=trim("`: display %10.3f r(estimate)'")
				local se11=trim("`: display %10.3f r(se)'")	
				
		
		drop highdamage*
		g highdamage=(totalapp>2.29e+07  & totalapp!=. & totalapp<3.74e+07 ) 
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)

			eststo t1c2
			
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c2
			
			
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta21=trim("`: display %10.3f r(estimate)'")
				local se21=trim("`: display %10.3f r(se)'")	
				
		
		drop highdamage*
		g highdamage=(totalapp>3.74e+07  & totalapp!=. & totalapp<7.78e+07) 
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)

		eststo t1c3
			
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c3
			
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta31=trim("`: display %10.3f r(estimate)'")
				local se31=trim("`: display %10.3f r(se)'")	
				
		
		drop highdamage*
		g highdamage=(totalapp>7.78e+07  & totalapp!=. & totalapp<1.31e+08) 
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)

		eststo t1c4
			
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c4
		
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta41=trim("`: display %10.3f r(estimate)'")
				local se41=trim("`: display %10.3f r(se)'")	
				
		
		
		drop highdamage*
		g highdamage=(totalapp>1.31e+08  & totalapp!=.) 
		mvencode highdamage, mv(0) o
		sort from_geoid move_year
		foreach i in 1 2 3 4 5{
				g highdamage_L`i'=highdamage[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
		}	
		foreach var in L1 L2 L3 L4 L5{
			label var highdamage_`var' "1(Storm year), `i'"
		}
		reghdfe ihs_migration i.highdamage, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)

		eststo t1c5
			
		reghdfe ihs_migration i.highdamage i.highdamage_L*, absorb(i.from_geoid  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t2c5
			
			
			
			lincom 1.highdamage+1.highdamage_L1+1.highdamage_L2+1.highdamage_L3+1.highdamage_L4+1.highdamage_L5
				local beta51=trim("`: display %10.3f r(estimate)'")
				local se51=trim("`: display %10.3f r(se)'")	
				

label var highdamage "1(Storm year)"				
				
	file open 	t 		using "$tables/tab_si18.tex", replace write
	file write t	"\begin{table}[htbp]\footnotesize \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
						"\caption{Migration impacts of high damage storms with per capita measures}\label{tab: total migration effects high damage pc}" _n  ///
						"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{10}{c}}" _n "\midrule" _n ///
						"Storm damage percentile:&90\$^{th}\$-95\$^{th}\$&95\$^{th}\$-97\$^{th}\$&97\$^{th}\$-99\$^{th}\$&99\$^{th}\$-99.5\$^{th}\$&Top 0.5\%&\\" _n ///
						"\midrule" _n 
	file close 	t
	
				esttab t1c* using "$tables/tab_si18.tex", l keep(1.highdamage) ///
				s(N, l("N") f(%11.0fc) lay(@)) $opts 
				
	file open 	t 		using "$tables/tab_si18.tex", append write
	file write t "\\" ///
							"\midrule																			" ///
							"Fixed Effects:									&		&		&		&		&					\\" ///
							"\hspace{3mm}County								&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\hspace{3mm}Year								&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\hspace{3mm}State\$\times\$Year				&Yes	&Yes	&Yes	&Yes	&Yes				\\ " ///
							"\midrule" _n "\end{tabular*}" _n ///
							"\begin{tabular*}{1\textwidth}{p{6in}}" _n ///
							"\footnotesize \textsc{Notes:} All columns report the results of a fixed effects specification with the IHS transformation of the number of out-migrants from a county as the outcome. Counties are defined as exposed to a storm if at least one hurricane resulted in a flood warning during the year or the county experienced wind speeds of at least 21 m/s - the speed at which structural damage begins to occur - during a hurricane in the year. In contrast to the approach in the main text, where bins are based on total compensation paid to residents of the county, here we calculate per capita compensation payments by county and divide storms into bins based on compensation per 100K residents approved by FEMA. These bins are based on the percentile of the compensated damage. The first bin is for storms from the 90\$^{th}\$-95\$^{th}\$ percentile, the second is the 95\$^{th}\$-97\$^{th}\$, the third is the 97\$^{th}\$-99\$^{th}\$, the fourth is the 99\$^{th}\$-99.5\$^{th}\$ and the fifth is the storms in the top 0.5\%. The selected percentiles correspond to those used for the total damage thresholds in the main text. \$^{*}\$p=0.1, \$^{**}\$p=0.05,\$^{***}\$p=0.01." ///
							"\end{tabular*}" _n "\end{table}" _n 
	file close 	t		
	
	
			
			
			